//
//  ARTWatchHistoryAlertView.m
//  ChartsDemo
//
//  Created by yunda on 2021/10/18.
//

#import "ARTWatchHistoryAlertView.h"

@interface ARTWatchHistoryAlertView()

@property (strong, nonatomic) UILabel *titleLabel;
// 富文本label
@property (strong, nonatomic)  YYLabel *contentLabel;

@end

@implementation ARTWatchHistoryAlertView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark--THE LIFE CYCLE
- (instancetype)initWithTitle:(NSString *)title messageAttribute:(NSAttributedString *)message{
    self = [super init];
    if (!self) {
        return nil;
    }
    self.frame = CGRectMake(0, 0, J_SCREEN_WIDTH, J_SCREEN_HEIGHT);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.1];
    
    self.viewBg.frame = CGRectMake(18, (J_SCREEN_HEIGHT-140)/2, J_SCREEN_WIDTH-18*2, 140);
    self.viewBg.backgroundColor = [UIColor clearColor];
    [self addSubview:self.viewBg];
    
    UIView *view = [[UIView alloc]initWithFrame:J_Frame(0, 0, self.viewBg.v_w, 20)];
    view.backgroundColor = [UIColor whiteColor];
    [self.viewBg addSubview:view];
    
    [view addSubview:self.titleLabel];
    [view addSubview:self.contentLabel];
    
    _titleLabel.frame = J_Frame(10, 20, view.v_w-10*2, 20);
    _contentLabel.frame = J_Frame(30, _titleLabel.v_MaxY+30, view.v_w-30*2, 20);
    
    _titleLabel.text = title;
    _contentLabel.attributedText = message;
    YYTextContainer  *titleContarer = [YYTextContainer new];
    titleContarer.size = CGSizeMake(_contentLabel.v_w,CGFLOAT_MAX);
    YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:titleContarer text:message];
    CGSize size = titleLayout.textBoundingSize;
    _contentLabel.v_h = size.height;
    
    view.v_h = _contentLabel.v_MaxY+40;
    J_ViewRadius(view, 20);
    
    UIButton *cancle = [UIButton buttonWithType:UIButtonTypeCustom];
    cancle.frame = J_Frame((self.viewBg.v_w-40)/2, view.v_MaxY+20, 40, 40);
    [cancle setImage:J_ImageNamed(@"popup_ic_close") forState:UIControlStateNormal];
    [cancle addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBg addSubview:cancle];
    
    self.viewBg.v_h = cancle.v_MaxY;
    self.viewBg.v_y = (J_SCREEN_HEIGHT-self.viewBg.v_h)/2;
    
    self.animationType = PopAnimationType_Center;
    return self;
}

- (void)cancleAction{
    [self dismiss];
}

#pragma mark--GETTER
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.numberOfLines = 0;
        _titleLabel.adjustsFontSizeToFitWidth = YES;
        _titleLabel.textColor = J_ColorFromHEX(0x0095B6);
        _titleLabel.font = J_MediumFont(18);
    }
    return _titleLabel;
}

- (YYLabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [[YYLabel alloc] init];
        _contentLabel.displaysAsynchronously = YES;
        _contentLabel.numberOfLines = 0;
        _contentLabel.backgroundColor = [UIColor whiteColor];
        _contentLabel.font = J_Font(14);
    }
    return _contentLabel;
}

@end
